import time
import json

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait


class ZhiHu:
    def __init__(self):
        self.url = 'https://www.zhihu.com/search?type=topic&q=' + '陌陌'
        self.chrome_options = Options()
        self.chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")  #  前面设置的端口号
        self.browser = webdriver.Chrome(executable_path=r'C:\Program Files\Python38\Scripts\chromedriver.exe', options=self.chrome_options)  # executable执行webdriver驱动的文件

    def get_start(self):
        self.browser.get(self.url)
        # time.sleep(20)  # 可以选择手动登录或者是自动化，我这里登录过就直接登陆了
        time.sleep(3)

        print(self.browser.page_source)
        # searchMain = self.browser.find_element_by_id("SearchMain")
        # if (searchMain != 'null'):
        #     attr = searchMain.get_attribute("data-za-detail-view-path-index", "0")
        #     if (attr != 'null' ):
        #         h2Eles = attr.get
                # Elements
                # h2Eles = attr.getElementsByTag("h2");
                # if (CollUtil.isNotEmpty(h2Eles)) {
                # Element headEle = h2Eles.get(0);
                # Element topicLink = headEle.getElementsByClass("TopicLink").get(0);
                # String href = "https:" + topicLink.attr("href");
                # String text = topicLink.getElementsByClass("Highlight").get(0).text();
                # // searchResult.setSearchText(text);
                # // searchResult.setUrl(href);
                # if (SpiderUtils.isContains(text, brand)){
                # result[0] = new String[]{brand, text, href};
                # }
                # // log.info("知乎:[{}],地址:[{}]", text, href);
                # }
        info = self.browser.get_cookies()  # 获取cookies
        print(info)
        # with open(r"..\download_txt\info.json", 'w', encoding='utf-8') as f:
        #     f.write(json.dumps(info))


if __name__ == '__main__':
    zhihu = ZhiHu()

    zhihu.get_start()